package com.rchat.pocmini.service.ping;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by ljd on 2017/5/19.
 * <p>
 * ping
 */

public class PingManager {

    //要ping的服务器ip地址
    public static final String PING_IP = "121.199.44.234";
    //一次ping的访问次数
    public static final int PING_COUNT = 1;
    //ping的大小
    public static final int PING_SIZE = 0;
    //ping的超时时间
    public static final int PING_TIME_OUT = 15;//秒
    //两次ping的间隔时间
    public static final int PING_INTERVAL = 60000;//毫秒

    private static final String TAG = "PingManager";
    private PingResultListener pingResultListener;

    public void setPingResultListener(PingResultListener listener) {
        pingResultListener = listener;
    }

    public void startPingThread(final String host, final int pingCount, final int timeOut, final int size) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                boolean pingResult = ping(host, pingCount, timeOut, size);
                pingResultListener.pingResult(pingResult);
            }
        }.start();
    }

    private boolean ping(String host, int pingCount, int timeOut, int size) {
        String line = null;
        Process process = null;
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader successReader = null;
        BufferedReader errorReader = null;
        String command = "ping -c " + pingCount + " -w " + timeOut + " -s " + size + " " + host;
        boolean isSuccess = false;
        try {
            process = Runtime.getRuntime().exec(command);
            if (process == null) {
//                Log.e(TAG,"ping fail:process is null.");
                append(stringBuffer, "ping fail:process is null.");
                return false;
            }
            successReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = successReader.readLine()) != null) {
//                Log.e(TAG,line);
                append(stringBuffer, line);

            }
            errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = errorReader.readLine()) != null) {
//                Log.e(TAG,line);
                append(stringBuffer, line);

            }
            int status = process.waitFor();
            if (status == 0) {
//                Log.e(TAG,"exec cmd success:" + command);
                append(stringBuffer, "exec cmd success:" + command);
                isSuccess = true;
            } else {
//                Log.e(TAG,status + "");
//                Log.e(TAG,"exec cmd fail.");
                append(stringBuffer, "exec cmd fail.");
                isSuccess = false;
            }
//            Log.e(TAG,"exec finished.");
            append(stringBuffer, "exec finished.");
        } catch (IOException e) {
        } catch (InterruptedException e) {
        } finally {
            if (process != null) {
                process.destroy();
            }
            if (successReader != null) {
                try {
                    successReader.close();
                } catch (IOException e) {
//                    Log.e(TAG,e.getMessage());
                }
            }
        }
        return isSuccess;
    }

    private static void append(StringBuffer stringBuffer, String text) {
        if (stringBuffer != null) {
            stringBuffer.append(text + "\n");
        }
    }
}
